package com.njym.java.security.admin.rest;

import com.alibaba.fastjson.JSON;
import com.njym.java.security.admin.biz.AnswertblBiz;
import com.njym.java.security.admin.biz.QuestiontblBiz;
import com.njym.java.security.admin.entity.Answertbl;
import com.njym.java.security.admin.entity.QuestionblVo;
import com.njym.java.security.admin.entity.Questiontbl;
import com.njym.java.security.common.msg.ObjectRestResponse;
import com.njym.java.security.common.rest.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("questiontbl")
public class QuestiontblController extends BaseController<QuestiontblBiz, Questiontbl> {

    @Autowired
    private AnswertblBiz answertblBiz;

    /**
     * 添加问题
     * @param data
     * @return
     */
    @RequestMapping(value = "/addObj", method = RequestMethod.POST)
    @ResponseBody
    public Object addObj(@RequestBody String data){
        Questiontbl entity = JSON.parseObject(data, Questiontbl.class);
        try {
            baseBiz.insertSelective(entity);
            return new ObjectRestResponse<>().rel(true);
        }catch (RuntimeException e){
            return new ObjectRestResponse<>().rel(false);
        }
    }

    /**
     * 获取问题列表
     * @param params
     * @return
     */
    @RequestMapping(value = "/getList", method = RequestMethod.GET)
    @ResponseBody
    public Object getList(@RequestParam Map<String, Object> params){
        int investId = Integer.parseInt(params.get("investId").toString());
        List<QuestionblVo> list = baseBiz.getList(investId);
        list.forEach( ques -> {
            Example example = new Example(Answertbl.class);
            example.createCriteria().andEqualTo("deleted",0).andEqualTo("questionId",ques.getId());
            List<Answertbl> answers = answertblBiz.selectByExample(example);
            ques.setAnswerOptions(answers);
        });
        Map<String,Object> data = new HashMap<>();
        data.put("list",list);
        return new ObjectRestResponse<>().rel(true).data(list);
    }

    /**
     * 问题添加选项
     * @param data
     * @return
     */
    @RequestMapping(value = "/addOption", method = RequestMethod.POST)
    @ResponseBody
    public Object addOption(@RequestBody String data){
        Answertbl entity = JSON.parseObject(data, Answertbl.class);
        try {
            answertblBiz.insertSelective(entity);
            return new ObjectRestResponse<>().rel(true);
        }catch (RuntimeException e){
            return new ObjectRestResponse<>().rel(false);
        }
    }

    /**
     * 修改问题和问题选项
     * @param data
     * @return
     */
    @RequestMapping(value = "/modify", method = RequestMethod.POST)
    @ResponseBody
    public Object modify(@RequestBody String data){
        QuestionblVo entity = JSON.parseObject(data, QuestionblVo.class);
        try {
            Questiontbl questiontbl = new Questiontbl();
            List<Answertbl> options = entity.getAnswerOptions();

            questiontbl.setId(entity.getId());
            questiontbl.setDeleted(entity.getDeleted());
            questiontbl.setQuestionName(entity.getQuestionName());
            questiontbl.setInvestId(entity.getInvestId());
            questiontbl.setQuestionType(entity.getQuestionType());
            questiontbl.setScore(entity.getScore());
            questiontbl.setQuestionOrder(entity.getQuestionOrder());
            baseBiz.updateSelectiveById(questiontbl);
            options.forEach(answer ->{
                answertblBiz.updateSelectiveById(answer);
            });
            return new ObjectRestResponse<>().rel(true);
        }catch (RuntimeException e){
            return new ObjectRestResponse<>().rel(false);
        }
    }

    /**
     * 根据问题id获取问题和选项
     * @param params
     * @return
     */
    @RequestMapping(value = "/getOneById", method = RequestMethod.GET)
    @ResponseBody
    public Object getOneById(@RequestParam Map<String, Object> params){
        try {
            int questionId = Integer.parseInt(params.get("id").toString());
            QuestionblVo item = baseBiz.getOneById(questionId);
            Example example = new Example(Answertbl.class);
            example.createCriteria().andEqualTo("deleted",0).andEqualTo("questionId",item.getId());
            List<Answertbl> answers = answertblBiz.selectByExample(example);
            item.setAnswerOptions(answers);
            return new ObjectRestResponse<>().rel(true).data(item);
        }catch (RuntimeException e){
            return new ObjectRestResponse<>().rel(false);
        }

    }



}